class Api::V1::SessionsController < Api::V1::BaseController
  def create
    @user = User.find_by(email: create_params[:email])

    if @user && @user.authenticate(create_params[:password])
      self.current_user = @user
      # 返回结果
      api_success({:status => 201, :data => {:id => @user.id, :name => @user.name, :admin => @user.admin, :authentication_token => @user.authentication_token}})
    else
      api_error({ :code => 50002, :status => 401, :msg => "用户认证失败"})
    end
  end

  private

  def create_params
    params.require(:user).permit(:email, :password)
  end
end
